from datetime import datetime
from x_com import source_api


def convert_to_timestamp(num_str: str) -> float:
    """
    将类似 '20241105093003292' 格式的字符串转换为时间戳。
    
    参数:
        num_str (str): 输入的时间字符串（例如 '20241105093003292'）。
    
    返回:
        float: 对应的时间戳。
    """
    # 提取日期和时间部分
    date_str = num_str[:8]  # 20241105 (年-月-日)
    time_str = num_str[8:-1]  # 0930032 (时:分:秒.毫秒)
    
    # 构造 datetime 对象
    date_time_str = f"{date_str} {time_str[:-3]}.{time_str[-3:]}"  # 转换为标准时间格式
    dt = datetime.strptime(date_time_str, "%Y%m%d %H%M%S.%f")
    
    # 获取时间戳
    return dt.timestamp()


def market_to_str(market: source_api.Market)->str:
  if market == source_api.Market.SZSE:
    return "SZSE"
  elif market == source_api.Market.SSE:
    return "SSE"
  elif market == source_api.Market.SHFE:
    return "SHFE"
  elif market == source_api.Market.DCE:
    return "DCE"
  elif market == source_api.Market.CZCE:
    return "CZCE"
  elif market == source_api.Market.CFFEX:
    return "CFFEX"
  elif market == source_api.Market.INE:
    return "INE"
  elif market == source_api.Market.GFEX:
    return "GFEX"
  else:
    return ""